/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vistas;

import Modelo.Clientes;
import Modelo.Funciones;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Carlita
 */
public class VerClientes extends javax.swing.JInternalFrame {

    DefaultTableModel modeloTbClientes = new DefaultTableModel();

    /**
     * Creates new form Clientes
     */
    public VerClientes() {
        initComponents();
        this.poblarTbClientes();
        //desactivo botones
        btEditar.setEnabled(false);
        btBorrar.setEnabled(false);
    }

    private void poblarTbClientes() {
        //lo defino de nuevo para que borre todos los elementos que tiene
        //Sobreescribo el metodo para que no se pueda editar ningun campo
        modeloTbClientes = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        //Cabeceras    
        modeloTbClientes.addColumn("Rut");
        modeloTbClientes.addColumn("Nombre");
        modeloTbClientes.addColumn("Teléfono");
        modeloTbClientes.addColumn("E-mail");
        modeloTbClientes.addColumn("Deuda");
        modeloTbClientes.addColumn("Ciudad");

        //Body, obtengo los clientes
        Clientes c = new Clientes();
        for (Object[] cliente : c.getClientes()) {
            //Defino la deuda como sí o no
            if (cliente[4].equals("1")) {
                cliente[4] = "Si";
            } else {
                cliente[4] = "No";
            }
            modeloTbClientes.addRow(cliente);
        }
        tbClientes.setModel(modeloTbClientes);
        //Agrego al modelo para que no se pueda editar
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientes = new javax.swing.JTable();
        btBorrar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Clientes");

        tbClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbClientesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbClientes);

        btBorrar.setText("Borrar Cliente");
        btBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBorrarActionPerformed(evt);
            }
        });

        btEditar.setText("Editar Cliente");
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(btEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                        .addComponent(btBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(127, 127, 127))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btBorrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btEditar, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        // TODO add your handling code here:
        if (tbClientes.getSelectedRow() > -1) {
            //envio al buscar cliente el cliente a modificar
            String rut = String.valueOf(tbClientes.getValueAt(tbClientes.getSelectedRow(), 0));
            Clientes c = new Clientes();
            //Si existe el cliente
            if (c.existeCliente(rut)) {
                BuscarCliente b = new BuscarCliente(rut);
                Principal.Escritorio.add(b);
                b.show();
            } else {
                Funciones.mensaje("El cliente no existe");
                this.poblarTbClientes();
            }
        } else {
            Funciones.mensaje("Debe seleccionar un cliente");
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBorrarActionPerformed
        if (tbClientes.getSelectedRow() > -1) {
            String codigo = String.valueOf(tbClientes.getValueAt(tbClientes.getSelectedRow(), 0));
            Clientes c = new Clientes();
            //Si existe el cliente
            if (c.existeCliente(codigo)) {
                c.deleteCliente(codigo);
                poblarTbClientes();
                btBorrar.setEnabled(false);
                btEditar.setEnabled(false);
            } else {
                Funciones.mensaje("El cliente no existe");
                this.poblarTbClientes();
            }

        }
    }//GEN-LAST:event_btBorrarActionPerformed

    private void tbClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbClientesMouseClicked
        //Activo botones
        btEditar.setEnabled(true);
        btBorrar.setEnabled(true);
    }//GEN-LAST:event_tbClientesMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBorrar;
    private javax.swing.JButton btEditar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbClientes;
    // End of variables declaration//GEN-END:variables
}
